package com.dousb.cloudlibrary.controller;

import com.dousb.cloudlibrary.common.BaseResponse;
import com.dousb.cloudlibrary.common.ErrorCode;
import com.dousb.cloudlibrary.common.ResultUtils;
import com.dousb.cloudlibrary.service.UserService;
import com.dousb.cloudlibrary.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 文件接口
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Resource
    private UserService userService;

    @Resource
    private MinioUtil minioUtil;

    /**
     * 文件上传
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload")
    public BaseResponse uploadFile(@RequestParam MultipartFile multipartFile) {
        try {
            Map<String, String> fileInfo = minioUtil.uploadFile(multipartFile);
            if (fileInfo != null) {
                return ResultUtils.success(fileInfo);
            } else {
                return ResultUtils.error(ErrorCode.PARAMS_ERROR, "文件上传失败");
            }
        } catch (Exception e) {
            log.error("文件上传异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "文件上传异常");
        }
    }
}
